package com.adminsystem.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.adminsystem.entity.ExamPaperJoinQuestion;
import com.adminsystem.entity.custom.RequestObject;
import com.adminsystem.service.ExamPaperJoinQuestionService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.adminsystem.entity.ExamPaperManage;
import com.adminsystem.service.IExamPaperManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷管理Controller
 *
 * @author admin
 * @date 2023-11-26
 */
@RestController
@RequestMapping("/examManage/paperManage")
public class ExamPaperManageController extends BaseController {
    @Autowired
    private IExamPaperManageService examPaperManageService;

    @Autowired
    private ExamPaperJoinQuestionService examPaperJoinQuestionService;

    /**
     * 查询试卷管理列表
     */
    @PreAuthorize("hasAuthority('examManage:paperManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaperManage examPaperManage) {
        startPage();
        List<ExamPaperManage> list = examPaperManageService.selectExamPaperManageList(examPaperManage);
        return getDataTable(list);
    }

    /**
     * 导出试卷管理列表
     */
    @PreAuthorize("hasAuthority('examManage:paperManage:export')")
    @Log(title = "试卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPaperManage examPaperManage) {
        List<ExamPaperManage> list = examPaperManageService.selectExamPaperManageList(examPaperManage);
        ExcelUtil<ExamPaperManage> util = new ExcelUtil<ExamPaperManage>(ExamPaperManage.class);
        util.exportExcel(response, list, "试卷管理数据");
    }

    /**
     * 获取试卷管理详细信息
     */
    @PreAuthorize("hasAuthority('examManage:paperManage:query')")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId) {
        return success(examPaperManageService.selectExamPaperManageByPaperId(paperId));
    }

    /**
     * 新增试卷管理
     */
    @PreAuthorize("hasAuthority('examManage:paperManage:add')")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPaperManage examPaperManage) {
        return toAjax(examPaperManageService.insertExamPaperManage(examPaperManage));
    }

    /**
     * 新增试卷管理
     */
    @Transactional
    @PreAuthorize("hasAuthority('examManage:paperManage:add')")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @PostMapping("/addQuestionIntoPaper")
    public AjaxResult addQuestionIntoPaper(@RequestBody RequestObject requestObject) {
        ExamPaperManage examPaperManage = examPaperManageService.getById(requestObject.getPaperId());
        examPaperManage.setPaperAnswer(requestObject.getPaperAnswer());
        examPaperManageService.updateById(examPaperManage);
        return toAjax(examPaperJoinQuestionService.saveBatch(requestObject.getPaperJoinQuestionList()));
    }

    /**
     * 修改试卷管理
     */
    @PreAuthorize("hasAuthority('examManage:paperManage:edit')")
    @Log(title = "试卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPaperManage examPaperManage) {
        return toAjax(examPaperManageService.updateExamPaperManage(examPaperManage));
    }

    /**
     * 删除试卷管理
     */
    @Transactional
    @PreAuthorize("hasAuthority('examManage:paperManage:remove')")
    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds) {
        examPaperJoinQuestionService.removeByIds(Arrays.stream(paperIds).collect(Collectors.toList()));
        return toAjax(examPaperManageService.deleteExamPaperManageByPaperIds(paperIds));
    }

    /**
     * 删除试卷ID删除试题
     */
    @Transactional
    @PreAuthorize("hasAuthority('examManage:paperManage:remove')")
    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
    @PostMapping("/batchRemovePaperQuestion")
    public AjaxResult removeByPaperAndQuestionId(@RequestBody List<ExamPaperJoinQuestion> examPaperJoinQuestionList) {
        return toAjax(examPaperManageService.removeByPaperAndQuestionId(examPaperJoinQuestionList));
    }
}
